package com.ocn.li.scmdy.modules.app.utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.alibaba.fastjson.JSON;
import com.ocn.li.scmdy.modules.app.entity.CoinDetailEntity;

/**
 * TODO
 *
 * @author crazypenguin
 * @version 1.0.0
 * @createdate 2019/9/19
 */
public class RsaUtils {

    /**
     * 根据密文和私钥返回明文
     * @param encrypt
     * @param privateKey
     * @return
     */
    public static String decode(String encrypt, String privateKey){
        try{
            RSA rsa = new RSA(privateKey,null);

            byte[] x = Base64.decode(encrypt);
            byte[] decrypt = rsa.decrypt(x, KeyType.PrivateKey);
            return StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8);
        }catch(Exception e){
            e.printStackTrace();
        }


        return null;
    }

    public static void main(String[] args) {

        String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKmx9FIV/bYdId6iihfN90QgMLMs8Gre35O9ywj+9D8OguCl2oaKU3nP5hGZyFrI0o9VRUlilvjNlCMUvE18gJ7obu2M6Uu5+shNIxo5fB45Bz5RMnwMW14WFspP+LCP9pg2AMb9c9vFQHiQlhZxRueI6vnCHnn/ca2fPqTWeICDAgMBAAECgYBG6nNsCnyoqqwI2mxTXmPDOKChgFpIjzNoDpzW+ZUg9POE5krYht4zvcor+NRd/Cbid4QpEuZ8u4z4zCWVTD6DerggBscG9w3BJDLCVrbs1uHW7OVOuAT4Vx82YG5Gzd7Q0qdhGSm8tCT3gQx7wBGjSDURZG/+NPwZKnHZJehrwQJBAOquG/ReV+b0pQUXUMYGmYxM/S75fPU5GcAZlg3fQRu323x6yy17B4d0c7rlqtTbpRqoj6UQI2a+7NXgSiUIUukCQQC5HIHkv+p1K/kp39TgzxsI8LZ4lpV0fwZYPJBZiTJ4ypU6gyT44tO83YUiOUrtnTd++AiXATo/KxZFSKKhkhyLAkAtkRLidvMQaA4ml+laGev0fvpmBZFKuASU9SLanK+ypxdZ6ynKMeo7VdQmxPNPkzpSm8oQXbrR915bBMrAuAYJAkBKK1z0Xd1Ac40TNScjqHgJngFO0e8yTU8qfaHM7kb11R3z4uYmfyQdsZiNfO1DxdGdox41+vzuoIH84ryfBl8/AkB7VWsYii5vDPuQBWpsk2mpVR9uwbQ/GuEhVxp0GZ2/rABKb2JsWyldTp/vWn4zowUa8L7smmULAUsFSQcEBLlE";

        String puiblicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCpsfRSFf22HSHeoooXzfdEIDCzLPBq3t+TvcsI/vQ/DoLgpdqGilN5z+YRmchayNKPVUVJYpb4zZQjFLxNfICe6G7tjOlLufrITSMaOXweOQc+UTJ8DFteFhbKT/iwj/aYNgDG/XPbxUB4kJYWcUbniOr5wh55/3Gtnz6k1niAgwIDAQAB";

        CoinDetailEntity coin  = new CoinDetailEntity();
        coin.setCoin(31L);
        coin.setDesc("ewewew");
        coin.setMark("eqqqq");
        coin.setNickname("zss");
        coin.setTerminal("app");
        coin.setType("获得");

        String str = JSON.toJSONString(coin);

//        String str = "15861696355";
        //公钥加密，私钥解密
        RSA rsa1 = new RSA(null,puiblicKey);
        RSA rsa2 = new RSA(privateKey,null);

        String encrypt = rsa1.encryptBase64(str, KeyType.PublicKey);

        System.out.println(encrypt);

        byte[] x = Base64.decode(encrypt);
        byte[] decrypt = rsa2.decrypt(x, KeyType.PrivateKey);

//        byte[] decrypt = decryptByPrivateKey(Base64.decode(encrypt.getBytes()), privateKey);
        System.out.println(StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
    }
}
